﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using log4net;
using ScaffoldDemo.AutoMapper;
using ScaffoldDemo.Entities;
using ScaffoldDemo.Models;
using ScaffoldSystem.Entities;
using ScaffoldSystem.Services.Abstract;
using ScaffoldSystem.Services.Concrete;

namespace ScaffoldWeb.Controllers
{
    public class UserController : Controller
    {
        private static ILog log = LogManager.GetLogger(typeof(UserController));

        private ISysUserService sysUserService;
        private ISysDeptService sysDeptService;

        public UserController(ISysUserService sysUserService, ISysDeptService sysDeptService)
        {
            this.sysUserService = sysUserService;
            this.sysDeptService = sysDeptService;
        }

        public JsonResult FindUserById(int userId) 
        {
            SysUser user = sysUserService.FindUserById(userId);
            log.InfoFormat("查询到用户，名称:{0}", user.Name);
            SysUserModel userModel = AutoMapperConfig.Mapper.Map<SysUser, SysUserModel>(user);

            if (user.DeptId != 0)
            {
                SysDept dept = sysDeptService.FindDeptById(user.DeptId);
                userModel.DeptName = dept.Name;
            }
            return Json(userModel, JsonRequestBehavior.AllowGet);
        }

       
    }
}